package lib.wxtt.demo;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.fast.dev.frame.Adapter.listview.AdapterHolder;
import com.fast.dev.frame.Adapter.listview.ListBaseAdapter;
import com.fast.dev.frame.BaseActivity;
import com.fast.dev.frame.DBUtils;
import com.fast.dev.frame.ui.Bind;
import com.fast.dev.frame.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity{

    @Bind(id = R.id.lv)
    ListView lv;
    @Bind(id = R.id.tv)
    TextView tv;
    @Bind(id = R.id.et_name)
    EditText et_name;
    @Bind(id = R.id.et_age)
    EditText et_age;

    private MyAdapter mAdapter;
    private ArrayList<PersonBean> mDatas;

    private DBUtils db;


    @Override
    public int setRootView() {
        return R.layout.activity_main;
    }

    @Override
    public void onInit(Bundle bundle) {
        db = DBUtils.create("fly",true);
        lv.setEmptyView(tv);
        mDatas = new ArrayList<>();
        mAdapter = new MyAdapter(lv,mDatas,R.layout.item_person);
        lv.setAdapter(mAdapter);
    }

    @Override
    public void clickView(View v) {

    }

    class MyAdapter extends ListBaseAdapter<PersonBean>{

        public MyAdapter(AbsListView view, List<PersonBean> mDatas, int itemLayoutId) {
            super(view, mDatas, itemLayoutId);
        }

        @Override
        public void convert(AdapterHolder holder, PersonBean item, boolean isScrolling, int position) {
            holder.setText(R.id.tv_name,"姓名："+item.getName());
            holder.setText(R.id.tv_age,"年龄："+item.getAge());
        }
    }

    PersonBean bean = null;
    /**
     * 说明：增加
     * @param view
     */
    public void add(View view){
        if (StringUtils.isEmpty(et_age.getText().toString()) || StringUtils.isEmpty(et_name.getText().toString())){
            show("输入数据");
        }else {
            bean = new PersonBean();
            bean.setName(et_name.getText().toString());
            bean.setAge(Integer.parseInt(et_age.getText().toString()));
            db.save(bean);
            show("添加成功");
        }
    }

    private void show(String str){
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    /**
     * 说明：删除
     * @param view
     */
    public void del(View view){
        db.delete(bean);
        show("删除成功");
    }

    /**
     * 说明：修改
     * @param view
     */
    public void update(View view){
        bean.setAge(Integer.parseInt(et_age.getText().toString()));
        db.update(bean);
    }

    /**
     * 说明：查询
     * @param view
     */
    public void select(View view){
        List<PersonBean> list;
        if (StringUtils.isEmpty(et_name.getText().toString())){
            list = db.findAll(PersonBean.class);

        }else {
            list = db.findAllByWhere(PersonBean.class,"name="+et_name.getText().toString());
        }
        mDatas.clear();
        mDatas.addAll(list);
        mAdapter.refresh(mDatas);
    }

}
